package com.jcl.recruit.wechat.logic;

import com.jcl.recruit.wechat.mapper.WeixinConfigMapper;
import com.jcl.recruit.wechat.model.WeixinConfig;
import com.jcl.recruit.wechat.utils.WechatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class WeixinConfigLogic implements WeixinConfigMapper {

    @Autowired
    private WeixinConfigMapper weixinConfigMapper;

    public WeixinConfig getWeixinConfig(boolean isTicket) {
        WeixinConfig config = findOne(WechatUtils.APP_ID, WechatUtils.APP_SECRET);
        if (null == config) {
            config = createWeixinConfig(isTicket);
        } else {
            config = updateWeixinConfig(config, isTicket);
        }
        return config;
    }

    @Override
    public WeixinConfig findOne(String appId, String appSecret) {
        return weixinConfigMapper.findOne(appId, appSecret);
    }

    @Override
    public int add(WeixinConfig config) {
        return weixinConfigMapper.add(config);
    }

    @Override
    public int update(WeixinConfig config) {
        return weixinConfigMapper.update(config);
    }

    @Override
    public int updateToken(String appId, String accessToken, Long tokenOverTime) {
        return weixinConfigMapper.updateToken(appId, accessToken, tokenOverTime);
    }

    @Override
    public int updateTicket(String appId, String ticket, Long ticketOverTime) {
        return weixinConfigMapper.updateTicket(appId, ticket, ticketOverTime);
    }

    private WeixinConfig createWeixinConfig(boolean isTicket) {
        WeixinConfig config = new WeixinConfig();
        config.setAppId(WechatUtils.APP_ID);
        config.setAppSecret(WechatUtils.APP_SECRET);
        config.setAccessToken(WechatUtils.getAccessToken());
        config.setTokenOverTime(WechatUtils.getOverTime());
        config.setTokenUpdateTime(new Date());
        if (isTicket) {
            config.setTicket(WechatUtils.getTicket());
            config.setTicketOverTime(WechatUtils.getOverTime());
            config.setTicketUpdateTime(new Date());
        }
        // insert into db
        add(config);
        return config;
    }

    private WeixinConfig updateWeixinConfig(WeixinConfig config, boolean isTicket) {

        long new_times = System.currentTimeMillis() / 1000;
        int update_status = 0;
        if (null != config.getTokenOverTime()
                && null != config.getAccessToken()
                && new_times <= config.getTokenOverTime()) {
        } else {
            update_status += 1;
        }
        if (isTicket) {
            new_times = System.currentTimeMillis() / 1000;
            if (null != config.getTicketOverTime()
                    && null != config.getTicket()
                    && new_times <= config.getTicketOverTime()) {
            } else {
                update_status += 2;
            }
        }
        if (update_status == 1) {
            config.setAccessToken(WechatUtils.getAccessToken());
            config.setTokenOverTime(WechatUtils.getOverTime());
            updateToken(WechatUtils.APP_ID, config.getAccessToken(), config.getTokenOverTime());
        } else if (update_status == 2) {
            config.setTicket(WechatUtils.getTicket());
            config.setTicketOverTime(WechatUtils.getOverTime());
            updateTicket(WechatUtils.APP_ID, config.getTicket(), config.getTicketOverTime());
        } else if (update_status == 3) {
            config.setAccessToken(WechatUtils.getAccessToken());
            config.setTokenOverTime(WechatUtils.getOverTime());
            config.setTicket(WechatUtils.getTicket());
            config.setTicketOverTime(WechatUtils.getOverTime());
            update(config);
        }
        return config;
    }
}
